package anshe.widget
{
	import anshe.event.AnsheNetEvent;
	import anshe.struct.LoadingObject;
	import anshe.struct.SWFData;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.utils.ByteArray;

	public class AnsheServer extends AnsheSuperClient
	{
		private const ACS_SERVER_NAME:String	= "ACS_NetServer";		
		
		private var loadArray:Array;

		public function AnsheServer()
		{
			super("AnsheServerMenu");
			
			loadArray = new Array();
		}
	
		protected override function InitGame():void
		{
			super.InitGame();
			
			// trace("服务器端程序初始化完毕!!!!");
			
			if(this.space)
			{
				this.x = 0;
				this.y = 0;
				
				//	网络事件侦听
				this.space.acsNetSpace.addEventListener(AnsheNetEvent.LOAD_SWF_PRESS, onLoadSWfPress);
				this.space.acsNetSpace.addEventListener(AnsheNetEvent.LOAD_SWF_DONE, onLoadSWFDoneEvent);
			}
		}		
				
		public function LoadFiles(fileName:String):void
		{
			if(this.space)
			{				
				var swfByte:ByteArray = this.space.acsNetSpace.GetSWFByte(fileName);
				trace("当前已经游戏列表:" + this.space.gameArray);				
				if(swfByte)				
				{
					trace("找到数据包");
					if(AnsheGlobal.CheckSWFLoader(this.space.gameArray, fileName))
					{
						trace("检测结果为可以下载");
						this.space.gameArray.push(fileName);
						LoadGameData(swfByte);
					}
				}
				else
				{
					this.visible = false;
					
					//trace("传入的文件是： " + fileName);
					//trace("找到的space是: " + this.space);
					//trace("要检测的数组是：" + this.space.gameArray);
					//trace("数组长度是:" + this.space.gameArray.length);
					
					//**			
					if(AnsheGlobal.CheckSWFLoader(this.space.gameArray, fileName))
					{
						try
						{
							ShowMessage("开始文件的下载>>> " + fileName);							
							this.space.gameArray.push(fileName);
							
							var swfLoader:LoadingObject = new LoadingObject();
							swfLoader.swfName = fileName + ".swf";
							swfLoader.swfObject = new Loader();
							swfLoader.swfObject.contentLoaderInfo.addEventListener(Event.INIT, onInitSWFLoaderDone);				
							swfLoader.swfObject.loadBytes(this.space.byteLoader);						
							
							this.loadArray.push(swfLoader);
							
							this.space.acsNetSpace.DownloadSWF(swfLoader.swfName);
						}
						catch(e:*)
						{
							ShowMessage("准备下载文件时出错:" + e);
						}
					}//**/	
				}
			}
			else
			{
				ShowMessage("没有找到 AnsheSprite 类对象");
			}
		}		
		
		private function onInitSWFLoaderDone(e:Event):void
		{
			this.parent.addChild(e.target.content);
		}
		
		//	下载进度显示
		private function onLoadSWfPress(e:AnsheNetEvent):void
		{
			ShowMessage("StartLoading... " + e.data.length, e.data);

			try
			{
				var swfData:SWFData = e.data[0] as SWFData;							
				
				for(var i:int = 0; i < this.loadArray.length; i++)
				{
					var swfLoader:LoadingObject = this.loadArray[i];
					var spr:DisplayObject = swfLoader.swfObject.content;
					
					if(swfData.swfName.toLowerCase().indexOf(swfLoader.swfName.toLowerCase()) >= 0)
					{
						try
						{
							if(spr is AnsheSWFLoader)
							{
								(spr as AnsheSWFLoader).percent = swfData.precent
							}
						}
						catch(err:*)
						{
							trace("spr is : " + spr);
						}
					}
				}
			}
			catch(e:*)
			{
				ShowMessage("Loading data Error!");
			}
		}
		
		//	文件下载完毕
		private function onLoadSWFDoneEvent(e:AnsheNetEvent):void
		{	
			var swfData:SWFData = e.data[0] as SWFData;
			
			this.visible = true;
			
			//**			
			for(var i:int = 0; i < this.loadArray.length; i++)
			{
				try
				{
					var swfLoader:LoadingObject = this.loadArray[i];
					
					if(swfLoader && swfData)
					{
						if(swfData.swfName.toLowerCase().indexOf(swfLoader.swfName.toLowerCase()) >= 0)
						{
							if(Math.floor(swfData.precent) >= 100)
							{
								swfLoader.swfObject.content.parent.removeChild(swfLoader.swfObject.content);						
		
								LoadGameData(swfData.toByteArray());
														
								this.loadArray.splice(i, 1);
								break;
							}
							else
							{
								ShowMessage("数据不完整...");
							}
						}
					}
				}
				catch(err:*)
				{
					trace("find Error::", err);
				}
			} //**/
		}
		
		private function LoadGameData(db:ByteArray):void
		{
			var tLoad:Loader = new Loader();
			tLoad.contentLoaderInfo.addEventListener(Event.INIT, onInitSWFDone);						
			tLoad.loadBytes(db);
		}
		
		private function onInitSWFDone(e:Event):void
		{
			e.target.removeEventListener(Event.INIT, onInitSWFDone);
			
			if(this.space)
			{
				ShowMessage("下载完毕：：" + e.target);
				// trace("now space:: " + this.space.acsNetSpace);
				
				var spr:DisplayObject = e.target.content;
				this.parent.addChild(spr);
				
				if(spr is AnsheSuperClient)
				{
					(spr as AnsheSuperClient).server = this.space.acsNetSpace;
					(spr as AnsheSuperClient).InitGameListener();
				}
					
				this.y = 0;
						
				spr.x = 10;
				spr.y = 25;
						
				this.parent.addChild(this);
			}
		}	
	}
}